package pcb.views.library 
{
	import com.intel.utils.Debugger;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import pcb.views.uievents.SlideEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Enter extends MovieClip
	{
		public function Enter() 
		{
			Debugger.traceOut("NEW ENTER created");
			
			stage.focus = this;						
			
			
			createEventHandler();
			
			dispatchEvent(new Event(SlideEvent.INTERACTIVITY_CREATED, true));
			
		}
		
		internal function createEventHandler():void 
		{
			this.addEventListener(KeyboardEvent.KEY_UP, keyPress);
			this.addEventListener(Event.REMOVED, removed);
		}
		
		private function removed(e:Event):void 
		{
			removeListeners();
			this.removeEventListener(Event.REMOVED, removed);
		}
		
		internal function removeListeners():void 
		{
			if (this.hasEventListener(KeyboardEvent.KEY_UP)) {
				this.removeEventListener(KeyboardEvent.KEY_UP, keyPress);
			}
			stage.removeEventListener(KeyboardEvent.KEY_UP, keyPress);
		}
		
		internal function keyPress(e:KeyboardEvent):void {
			Debugger.traceOut("key pressed " + e.keyCode);
			if (e.keyCode == 13) {
				removeListeners();
				dispatchEvent(new Event(SlideEvent.INTERACTIVITY_COMPLETED, true));				
			}
		}
		
		public function addAnnotation(mcAnnotation:AnnotationMC):void 
		{	
			mcAnnotation.name = "mcAnnotation";
			this.parent.addChild(mcAnnotation);
		}
		
		public function getAnnotationMcPosition():DisplayObject {
			return this as DisplayObject;
		}
		
		public function showMe():void 
		{
			removeListeners();
			var sm:MovieClip = this.parent.getChildByName("mcShowMe") as MovieClip;
			if (sm != null) {
				if (this.parent.getChildByName("mcAnnotation")) {
					this.parent.removeChild(this.parent.getChildByName("mcAnnotation"));
				}
				sm.execute();			
			} else {
				Debugger.traceOut("could not find show me MC", Debugger.ERROR);
			}
		}
		
	}
	
}